package com.opencv.service;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.opencv.domain.entity.CgmHospitalDeptEntity;
import com.opencv.domain.entity.CgmHospitalEntity;
import com.opencv.domain.entity.CgmRegionCodeEntity;
import com.opencv.mapper.CgmRegionCodeMapper;
import com.opencv.mapper.HospitalDeptMapper;
import com.opencv.mapper.HospitalMapper;
import com.opencv.utils.ExcelUtil;
import com.opencv.utils.IdUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author qz.wu
 * @date 2021/8/26 14:42
 * @descriptions TODO
 */
@Service
public class ImportDataService {

    private static final String DIR_PATH = "E:\\backUp\\医院导入模板20210826.xlsx";

    @Autowired
    private HospitalMapper hospitalMapper;
    @Autowired
    private HospitalDeptMapper hospitalDeptMapper;
    @Autowired
    private CgmRegionCodeMapper regionCodeMapper;

    public String query() {
        Long id = 330324L;
        CgmRegionCodeEntity cgmRegionCodeEntity = regionCodeMapper.selectById(id);
        return JSONUtil.toJsonStr(cgmRegionCodeEntity);
    }

    public void importExcelData(String path) throws IOException {
        File file = new File(DIR_PATH);
        Workbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file);
        } catch (Exception ex) {
            workbook = new HSSFWorkbook(new FileInputStream(file));
        }
        int numberOfSheets = workbook.getNumberOfSheets();
        for (int i = 0; i < numberOfSheets; i++) {
            // 只导入第一个sheet页的数据
            if (i > 0)
                break;
            Sheet sheet = workbook.getSheetAt(i);
            int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
            for (int j = 0; j < physicalNumberOfRows; j++) {
                Row row = sheet.getRow(j);
                if (j == 0) {
                    continue;// 标题行
                }
                int physicalNumberOfCells = row.getPhysicalNumberOfCells();
                // 省级行政区
                String provideName = "";
                // 地级行政区
                String cityName = "";
                // 医院
                String hospitalName = "";
                // 科室
                String deptName = "";
                // 子科室
                String subDeptName = "";

                for (int k = 0; k < physicalNumberOfCells; k++) {
                    Object cellValue = ExcelUtil.getCellValue(row, k);
                    if (k == 1) {
                        provideName = (String) cellValue;
                    }
                    if (k == 2) {
                        cityName = (String) cellValue;
                    }
                    if (k == 3) {
                        hospitalName = (String) cellValue;
                    }
                    if (k == 4) {
                        deptName = (String) cellValue;
                    }
                    if (k == 5) {
                        subDeptName = (String) cellValue;
                    }
                }
                // 开始插入数据
                QueryWrapper<CgmRegionCodeEntity> regionCodeWrapper = new QueryWrapper<CgmRegionCodeEntity>();
                // 特殊处理的区
                if (cityName.equals("蓟州区")) {
                    regionCodeWrapper.eq("name", "蓟县");
                    regionCodeWrapper.eq("level", 3);
                } else if (cityName.equals("开州区")) {
                    regionCodeWrapper.eq("name", "开县");
                    regionCodeWrapper.eq("level", 3);
                } else if (cityName.equals("卢湾区")) {
                    regionCodeWrapper.eq("name", "黄浦区");
                    regionCodeWrapper.eq("level", 3);
                } else if (cityName.equals("兴安")) {
                    regionCodeWrapper.eq("name", "兴安盟");
                    regionCodeWrapper.eq("level", 2);
                } else if (this.isSecondLevel(cityName)) { // 市级的区
                    regionCodeWrapper.eq("name", cityName);
                    regionCodeWrapper.eq("level", 2);
                } else if (cityName.contains("区")) {
                    regionCodeWrapper.eq("name", cityName);
                    regionCodeWrapper.eq("level", 3);
                } else if (cityName.contains("县")) {
                    if (cityName.equals("石柱县")) {
                        regionCodeWrapper.eq("sname", "石柱");
                    } else {
                        regionCodeWrapper.eq("name", cityName);
                    }

                    regionCodeWrapper.eq("level", 3);
                } else if (this.isThirdLevel(cityName)) { // 县级市
                    regionCodeWrapper.eq("sname", cityName);
                    regionCodeWrapper.eq("level", 3);
                } else {
                    if (cityName.equals("甘南藏族自治州")) {
                        regionCodeWrapper.eq("sname", "甘南");
                    } else if (cityName.equals("临夏回族自治州")) {
                        regionCodeWrapper.eq("sname", "临夏");
                    } else if (cityName.equals("恩施土家族苗族自治州")) {
                        regionCodeWrapper.eq("sname", "恩施");
                    } else if (cityName.equals("阿坝藏族羌族自治州")) {
                        regionCodeWrapper.eq("sname", "阿坝");
                    } else if (cityName.equals("凉山彝族自治州")) {
                        regionCodeWrapper.eq("sname", "凉山");
                    } else if (cityName.equals("甘孜藏族自治州")) {
                        regionCodeWrapper.eq("sname", "甘孜");
                    } else if (cityName.equals("海西蒙古族藏族自治州")) {
                        regionCodeWrapper.eq("sname", "海西");
                    } else if (cityName.equals("黄南藏族自治州")) {
                        regionCodeWrapper.eq("sname", "黄南");
                    } else if (cityName.equals("黔东南苗族侗族自治州")) {
                        regionCodeWrapper.eq("sname", "黔东南");
                    } else if (cityName.equals("黔南布依族苗族自治州")) {
                        regionCodeWrapper.eq("sname", "黔南");
                    } else if (cityName.equals("黔西南布依族苗族自治州")) {
                        regionCodeWrapper.eq("sname", "黔西南");
                    } else if (cityName.equals("石柱土家族自治县")) {
                        regionCodeWrapper.eq("sname", "石柱");
                    } else if (cityName.equals("大理白族自治州")) {
                        regionCodeWrapper.eq("sname", "大理");
                    } else if (cityName.equals("西双版纳傣族自治州")) {
                        regionCodeWrapper.eq("sname", "西双版纳");
                    } else if (cityName.equals("德宏傣族景颇族自治州")) {
                        regionCodeWrapper.eq("sname", "德宏");
                    } else if (cityName.equals("楚雄彝族自治州")) {
                        regionCodeWrapper.eq("sname", "楚雄");
                    } else if (cityName.equals("怒江傈僳族自治州")) {
                        regionCodeWrapper.eq("sname", "怒江");
                    } else if (cityName.equals("文山壮族苗族自治州")) {
                        regionCodeWrapper.eq("sname", "文山");
                    } else if (cityName.equals("湘西土家族苗族自治州")) {
                        regionCodeWrapper.eq("sname", "湘西");
                    } else if (cityName.equals("果洛藏族自治州")) {
                        regionCodeWrapper.eq("sname", "果洛");
                    } else if (cityName.equals("海南藏族自治州")) {
                        regionCodeWrapper.eq("sname", "海南");
                    } else if (cityName.equals("红河哈尼族彝族自治州")) {
                        regionCodeWrapper.eq("sname", "红河");
                    } else if (cityName.equals("延边朝鲜族自治州")) {
                        regionCodeWrapper.eq("sname", "延边");
                    } else if (cityName.equals("巴音郭楞蒙古自治州")) {
                        regionCodeWrapper.eq("sname", "巴音郭楞");
                    } else if (cityName.equals("伊犁哈萨克自治州")) {
                        regionCodeWrapper.eq("sname", "伊犁");
                    } else if (cityName.equals("昌吉回族自治州")) {
                        regionCodeWrapper.eq("sname", "昌吉");
                    } else if (cityName.equals("博尔塔拉蒙古自治州")) {
                        regionCodeWrapper.eq("sname", "博尔塔拉");
                    } else if (cityName.equals("克孜勒苏柯尔克孜自治州")) {
                        regionCodeWrapper.eq("sname", "克孜勒苏");
                    } else if (cityName.equals("大理州")) {
                        regionCodeWrapper.eq("sname", "大理");
                    } else if (cityName.equals("凉山州")) {
                        regionCodeWrapper.eq("sname", "凉山");
                    } else {
                        regionCodeWrapper.eq("sname", cityName);
                    }

                    regionCodeWrapper.eq("level", 2);
                }

                // 1、插入医院数据
                CgmRegionCodeEntity regionCode = null;
                if (cityName.equals("蓟州区") || cityName.equals("开州区") || cityName.equals("卢湾区")) {
                    regionCode = regionCodeMapper.selectOne(regionCodeWrapper);
                } else if (cityName.equals("兴安")) {
                    regionCode = regionCodeMapper.selectOne(regionCodeWrapper);
                } else if (this.isSecondLevel(cityName)) { // 市级的区
                    regionCode = regionCodeMapper.selectOne(regionCodeWrapper);
                } else if (cityName.contains("区")) {
                    List<CgmRegionCodeEntity> list = regionCodeMapper.selectList(regionCodeWrapper);

                    if (list.size() == 1) {
                        regionCode = list.get(0);
                    } else if (list.size() == 0) {
                        if (cityName.equals("密云区")) {
                            regionCodeWrapper = new QueryWrapper<CgmRegionCodeEntity>();
                            regionCodeWrapper.eq("name", "密云县");
                            regionCodeWrapper.eq("level", 3);
                            regionCode = regionCodeMapper.selectOne(regionCodeWrapper);
                        } else if (cityName.equals("崇明区")) {
                            regionCodeWrapper = new QueryWrapper<CgmRegionCodeEntity>();
                            regionCodeWrapper.eq("name", "崇明县");
                            regionCodeWrapper.eq("level", 3);
                            regionCode = regionCodeMapper.selectOne(regionCodeWrapper);
                        } else if (cityName.equals("延庆区")) {
                            regionCodeWrapper = new QueryWrapper<CgmRegionCodeEntity>();
                            regionCodeWrapper.eq("name", "延庆县");
                            regionCodeWrapper.eq("level", 3);
                            regionCode = regionCodeMapper.selectOne(regionCodeWrapper);
                        } else {
                            if (cityName.contains("区")) {
                                regionCodeWrapper = new QueryWrapper<CgmRegionCodeEntity>();
                                regionCodeWrapper.eq("name", cityName.replace("区", "县"));
                                regionCodeWrapper.eq("level", 3);
                                regionCode = regionCodeMapper.selectOne(regionCodeWrapper);
                            } else
                                System.out.println(cityName);
                        }
                    } else {
                        QueryWrapper<CgmRegionCodeEntity> searchRegionCodeWrapper =
                                new QueryWrapper<CgmRegionCodeEntity>();
                        searchRegionCodeWrapper.eq("sname", provideName).eq("level", 1);
                        CgmRegionCodeEntity searchRegionCode = regionCodeMapper.selectOne(searchRegionCodeWrapper);
                        QueryWrapper<CgmRegionCodeEntity> cityRegionCodeWrapper =
                                new QueryWrapper<CgmRegionCodeEntity>();
                        searchRegionCodeWrapper.eq("pid", searchRegionCode.getId()).eq("level", 2);
                        List<CgmRegionCodeEntity> cityList = regionCodeMapper.selectList(cityRegionCodeWrapper);
                        for (CgmRegionCodeEntity cityItem : cityList) {
                            for (CgmRegionCodeEntity item : list) {
                                if (cityItem.getId().equals(item.getPid())) {
                                    regionCode = item;
                                    break;
                                }
                            }
                        }
                    }
                } else {
                    regionCode = regionCodeMapper.selectOne(regionCodeWrapper);
                }
                QueryWrapper<CgmHospitalEntity> hospitalWrapper =
                        new QueryWrapper<CgmHospitalEntity>().eq("name", hospitalName);
                CgmHospitalEntity cgmHospital = hospitalMapper.selectOne(hospitalWrapper);
                if (Objects.isNull(cgmHospital)) {
                    cgmHospital = new CgmHospitalEntity();
                    cgmHospital.setId(IdUtils.nextSnowFlakeId());
                    cgmHospital.setName(hospitalName);
                    cgmHospital.setCountry(100000L);
                    if (cityName.equals("蓟州区") || cityName.equals("开州区") || cityName.equals("卢湾区")) {
                        cgmHospital.setDistrict(Long.valueOf(regionCode.getId()));
                        cgmHospital.setCity(Long.valueOf(regionCode.getPid()));
                        QueryWrapper<CgmRegionCodeEntity> regionCodeWrapper2 = new QueryWrapper<CgmRegionCodeEntity>();
                        regionCodeWrapper2.eq("id", regionCode.getPid()).eq("level", 2);
                        CgmRegionCodeEntity regionCode2 = regionCodeMapper.selectOne(regionCodeWrapper2);
                        cgmHospital.setProvince(Long.valueOf(regionCode2.getPid()));
                    } else if (cityName.equals("兴安")) {
                        cgmHospital.setProvince(Long.valueOf(regionCode.getPid()));
                        cgmHospital.setCity(Long.valueOf(regionCode.getId()));
                    } else if (this.isSecondLevel(cityName)) { // 市级的区
                        cgmHospital.setProvince(Long.valueOf(regionCode.getPid()));
                        cgmHospital.setCity(Long.valueOf(regionCode.getId()));
                    } else if (cityName.contains("区")) {
                        cgmHospital.setDistrict(Long.valueOf(regionCode.getId()));
                        QueryWrapper<CgmRegionCodeEntity> regionCodeWrapper2 = new QueryWrapper<CgmRegionCodeEntity>();
                        regionCodeWrapper2.eq("id", regionCode.getPid()).eq("level", 2);
                        CgmRegionCodeEntity regionCode2 = regionCodeMapper.selectOne(regionCodeWrapper2);
                        cgmHospital.setCity(Long.valueOf(regionCode2.getId()));
                        cgmHospital.setProvince(Long.valueOf(regionCode2.getPid()));
                    } else if (cityName.contains("县")) {
                        cgmHospital.setDistrict(Long.valueOf(regionCode.getId()));
                        QueryWrapper<CgmRegionCodeEntity> regionCodeWrapper2 = new QueryWrapper<CgmRegionCodeEntity>();
                        regionCodeWrapper2.eq("id", regionCode.getPid()).eq("level", 2);
                        CgmRegionCodeEntity regionCode2 = regionCodeMapper.selectOne(regionCodeWrapper2);
                        cgmHospital.setCity(Long.valueOf(regionCode2.getId()));
                        cgmHospital.setProvince(Long.valueOf(regionCode2.getPid()));
                    } else if (this.isThirdLevel(cityName)) { // 县级市
                        cgmHospital.setDistrict(Long.valueOf(regionCode.getId()));
                        cgmHospital.setCity(Long.valueOf(regionCode.getPid()));
                        QueryWrapper<CgmRegionCodeEntity> regionCodeWrapper2 = new QueryWrapper<CgmRegionCodeEntity>();
                        regionCodeWrapper2.eq("id", regionCode.getPid()).eq("level", 2);
                        CgmRegionCodeEntity regionCode2 = regionCodeMapper.selectOne(regionCodeWrapper2);
                        cgmHospital.setProvince(Long.valueOf(regionCode2.getPid()));
                    } else {
                        cgmHospital.setProvince(Long.valueOf(regionCode.getPid()));
                        cgmHospital.setCity(Long.valueOf(regionCode.getId()));
                    }
                    cgmHospital.setCreateTime(new Date());
                    hospitalMapper.insert(cgmHospital);
                }

                // 2、新增科室数据 -- 父级科室
                QueryWrapper<CgmHospitalDeptEntity> hospitalDeptWrapper = new QueryWrapper<CgmHospitalDeptEntity>()
                        .eq("dept_name", deptName).isNull("parent_id").eq("hospital_id", cgmHospital.getId());
                CgmHospitalDeptEntity cgmHospitalDept = hospitalDeptMapper.selectOne(hospitalDeptWrapper);
                if (Objects.isNull(cgmHospitalDept)) {
                    cgmHospitalDept = new CgmHospitalDeptEntity();
                    cgmHospitalDept.setId(IdUtils.nextSnowFlakeId());
                    cgmHospitalDept.setHospitalId(cgmHospital.getId());
                    cgmHospitalDept.setHospitalName(cgmHospital.getName());
                    cgmHospitalDept.setDeptName(deptName);
                    cgmHospitalDept.setStatus(0);
                    cgmHospitalDept.setOrderNum(0);
                    cgmHospitalDept.setCreateTime(new Date());
                    hospitalDeptMapper.insert(cgmHospitalDept);
                }

                // 3、新增科室数据 -- 子级科室
                QueryWrapper<CgmHospitalDeptEntity> subHospitalDeptWrapper =
                        new QueryWrapper<CgmHospitalDeptEntity>().eq("parent_id", cgmHospitalDept.getId())
                                .eq("dept_name", subDeptName).eq("hospital_id", cgmHospital.getId());
                CgmHospitalDeptEntity subSearchHospitalDept = hospitalDeptMapper.selectOne(subHospitalDeptWrapper);
                if (Objects.isNull(subSearchHospitalDept)) {
                    CgmHospitalDeptEntity subHospitalDept = new CgmHospitalDeptEntity();
                    subHospitalDept.setId(IdUtils.nextSnowFlakeId());
                    subHospitalDept.setHospitalId(cgmHospital.getId());
                    subHospitalDept.setHospitalName(cgmHospital.getName());
                    subHospitalDept.setParentId(cgmHospitalDept.getId());
                    subHospitalDept.setDeptName(subDeptName);
                    subHospitalDept.setStatus(0);
                    subHospitalDept.setOrderNum(0);
                    subHospitalDept.setCreateTime(new Date());
                    hospitalDeptMapper.insert(subHospitalDept);
                }
            }
        }
    }

    // 县级市
    private boolean isThirdLevel(String cityName) {
        if (cityName.equals("潜江") || cityName.equals("天门") || cityName.equals("仙桃") || cityName.equals("济源")
                || cityName.equals("万宁") || cityName.equals("文昌") || cityName.equals("五指山") || cityName.equals("琼海")
                || cityName.equals("陵水") || cityName.equals("东方") || cityName.equals("儋州") || cityName.equals("乐东")
                || cityName.equals("保亭") || cityName.equals("石河子") || cityName.equals("阿拉尔") || cityName.equals("五家渠")
                || cityName.equals("巢湖") || cityName.equals("神农架")) {
            return true;
        }
        return false;
    }

    // 市级的区
    private boolean isSecondLevel(String cityName) {
        if (cityName.equals("两江新区") || cityName.equals("大兴安岭地区") || cityName.equals("阿里地区") || cityName.equals("阿勒泰地区")
                || cityName.equals("喀什地区") || cityName.equals("塔城地区") || cityName.equals("阿克苏地区")
                || cityName.equals("和田地区")) { // 市级的区
            return true;
        }
        return false;
    }
}
